package com.zyd.blog.personal.service.impl;

import com.zyd.blog.personal.enuminterface.IsDelete;
import com.zyd.blog.personal.enuminterface.Status;
import com.zyd.blog.personal.enuminterface.Step;
import com.zyd.blog.personal.po.CompanyInfoPo;
import com.zyd.blog.personal.service.*;
import com.zyd.blog.personal.utils.JavaBeanAndMapUtils;
import com.zyd.blog.personal.utils.StringUtils;
import com.zyd.blog.personal.utils.DateUtils;
import com.zyd.blog.personal.vo.ApplyDesignSettleVo;
import com.zyd.blog.personal.vo.MerchantInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 大平台总的业务逻辑层
 * @author mfy
 */
@Service
public class BigPlotformServiceImpl implements BigPlotformService {

    @Resource
    private UserNoService userNoService;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private UserCompanyService userCompanyService;

    @Resource
    private MerchantInfoService merchantInfoService;

    @Resource
    private ProtocolService protocolService;

    @Resource
    private UserApplyDesignTempService userApplyDesignTempService;

    @Resource
    private MyCompanyInfoService myCompanyInfoService;


    private Map<String,Object> getUserNoCondition(ApplyDesignSettleVo applyDesignSettleVo){
        Map<String,Object> userNoCondition = new HashMap<>(16);
        userNoCondition.put("id", applyDesignSettleVo.getUserId());
        userNoCondition.put("step", Step.STEP_SUBMIT_NOREVIEW);
        userNoCondition.put("isFirst", "Y");
        userNoCondition.put("applyTime", (new Date()).getTime());
        return userNoCondition;
    }

    private Map<String,Object> getCondition(ApplyDesignSettleVo applyDesignSettleVo){
        Map<String,Object> condition = JavaBeanAndMapUtils.javaBeanToMap(applyDesignSettleVo);
        String email = applyDesignSettleVo.getEmail();
        if(email == null || "".equals(email)){
            email = " ";
        }
        condition.put("step", Step.STEP_SUBMIT_NOREVIEW);
        condition.put("submitTime", (new Date()).getTime());
        condition.put("email", email);
        condition.put("birthday", DateUtils.parseStringToLong(applyDesignSettleVo.getBirthday()));
        condition.put("liveAddress",StringUtils.combinationAddress(null, applyDesignSettleVo.getProvinceLive(), applyDesignSettleVo.getCityLive(), applyDesignSettleVo.getAreaLive(), applyDesignSettleVo.getDetailedLive()));
        condition.put("workAddress",StringUtils.combinationAddress("", applyDesignSettleVo.getProvinceWork(), applyDesignSettleVo.getCityWork(), applyDesignSettleVo.getAreaWork(), applyDesignSettleVo.getDetailedWork()));
        condition.put("designType",applyDesignSettleVo.getDesignType());
        condition.put("serviceAddress",StringUtils.combinationString(applyDesignSettleVo.getServiceAddress()));
        condition.put("businessLicense",StringUtils.combinationString(applyDesignSettleVo.getBusinessLicense()));
        condition.put("designTime",DateUtils.parseStringToLong(applyDesignSettleVo.getDesignTime()));
        return condition;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int applyDesign(ApplyDesignSettleVo applyDesignSettleVo){
        List<Map<String,Object>> userNo = userNoService.getUserNoByCondition(applyDesignSettleVo.getUserId(), IsDelete.IS_DELETE_NO, Status.STATUS_YES);
        if(userNo == null || userNo.size() == 0){
            //  该用户不存在
            return -1;
        }
        String step = (String) userNo.get(0).get("step");
        switch (step){
            case Step.STEP_SUBMIT_NOREVIEW:
                //  已提交申请,等待审核
                return 2;
            case Step.STEP_REVIEW_YES:
                //  您已经是设计师,无需重复申请
                return 3;
            default:
                break;
        }

        Map<String,Object> condition = getCondition(applyDesignSettleVo);
        Boolean flag1 ;
        if(userApplyDesignTempService.isHaveUserApplyDesignTemp(applyDesignSettleVo.getUserId())){
            flag1 = userApplyDesignTempService.updateUserApplyDesignTemp(condition);
        }else{
            flag1 = userApplyDesignTempService.addUserApplyDesignTemp(condition);
        }

        Map<String,Object> userNoCondition = getUserNoCondition(applyDesignSettleVo);
        Boolean flag3 = userNoService.updateUserNo(userNoCondition);
        if(flag1 && flag3){
            //  提交申请成功
            return 0;
        }
        //  提交申请失败
        return 1;
    }

    @Override
    public int updateDesign(ApplyDesignSettleVo applyDesignSettleVo){
        List<Map<String,Object>> userNo = userNoService.getUserNoByCondition(applyDesignSettleVo.getUserId(), IsDelete.IS_DELETE_NO, Status.STATUS_YES);
        if(userNo == null || userNo.size() == 0){
            //  该用户不存在
            return -1;
        }

        Map<String,Object> condition = getCondition(applyDesignSettleVo);
        Boolean flag1 ;
        if (!userApplyDesignTempService.isHaveUserApplyDesignTemp(applyDesignSettleVo.getUserId())) {
            condition.put("isDelete", IsDelete.IS_DELETE_NO);
        }
        flag1 = userApplyDesignTempService.updateUserApplyDesignTemp(condition);

        Map<String,Object> userNoCondition = getUserNoCondition(applyDesignSettleVo);
        Boolean flag3 = userNoService.updateUserNo(userNoCondition);

        if(flag1 && flag3){
            //  提交申请成功
            return 0;
        }
        //  提交申请失败
        return 1;

    }

    private Map<String,Object> getApplyInfoOfDesign(Long userId){
        if(userId == null){
            return new HashMap<>(0);
        }
        Map<String,Object> result = new HashMap<>(16);
        Map<String,Object> userInfo = userInfoService.getUnitUserInfo(userId);
        result.put("userId",userId);

        Map<String,Object> userCompany = userCompanyService.getDesignerInfo(userId);
        //  添加所有的属性信息
        result.putAll(userInfo);
        result.putAll(userCompany);
        //  清除无需属性
        result.remove("teamName");
        result.remove("updateTimeInfo");
        result.remove("addTimeNo");
        result.remove("password");
        result.remove("updateTimeNo");
        result.remove("homeAddress");
        result.remove("settleTime");
        result.remove("isDelete");
        result.remove("username");
        result.remove("userType");
        result.remove("status");
        //   属性格式化
        Map<String,Object> liveAddress = StringUtils.splitAddress((String) result.get("liveAddress"));
        result.put("liveAddress",liveAddress);
        result.put("businessLicense",StringUtils.splitStr((String)result.get("businessLicense")));
        String birthday =  DateUtils.parseLongToString((Long) result.get("birthday"),"yyyy-MM-dd");
        result.put("birthday",birthday);
        String applyTime =  DateUtils.parseLongToString(Long.valueOf((String) result.get("applyTime")),"yyyy-MM-dd");
        result.put("applyTime",applyTime);
        String step = (String) result.get("step");
        result.put("isApply",true);
        switch (step){
            case "1":
                result.put("step","申请未提交");
                break;
            case "2":
                result.put("step","申请未审核");
                break;
            case "3":
                result.put("step","审核通过");
                break;
            case "4":
                result.put("step","审核失败");
                break;
            default:
                break;
        }
        return result;
    }

    @Override
    public Map<String,Object> getApplyInfo(Long userId){
        if(userId == null){
            return new HashMap<>(0);
        }
        Map<String,Object> result;
        Map<String,Object> userNo = userNoService.getUserNoByUserId(userId);
        String step = (String) userNo.get("step");
        switch (step){
            case "0":
                result = new HashMap<>(16);
                result.put("isApply","N");
                return result;
            case "3":
                result = getApplyInfoOfDesign(userId);
                result.put("isApply","Y");
                result.put("isSubmit","Y");
                result.put("isReView","Y");
                result.put("isSuccess","Y");
                return result;
            case "1":
                result = new HashMap<>(16);
                result.put("isApply","Y");
                result.put("isSubmit","N");
                break;
            case "2":
                result = new HashMap<>(16);
                result.put("isApply","Y");
                result.put("isSubmit","Y");
                result.put("isReView","N");
                break;
            case "4":
                result = new HashMap<>(16);
                result.put("isApply","Y");
                result.put("isSubmit","Y");
                result.put("isReView","Y");
                result.put("isSuccess","N");
                result.put("reason",userNo.get("reason"));
                break;
            default:
                result = new HashMap<>(0);
        }
        Map<String,Object> userApplyDesignInfo = userApplyDesignTempService.getUserApplyDesignTemp(userId);
        result.putAll(userApplyDesignInfo);
        result.put("birthday",DateUtils.parseLongToString((Long) result.get("birthday"),"yyyy-MM-dd"));
        result.remove("isDelete");
        result.remove("addTime");
        result.remove("updateTime");
        result.remove("submitTime");
        result.put("designTime",DateUtils.parseLongToString((Long) result.get("designTime"),"yyyy"));
        result.put("liveAddress",StringUtils.splitAddress((String) result.get("liveAddress")));
        result.put("workAddress",StringUtils.splitAddress((String) result.get("workAddress")));
        result.put("businessLicense",StringUtils.splitStr((String)result.get("businessLicense")));
        result.put("serviceAddress",StringUtils.splitStr((String) result.get("serviceAddress")));
        return result;
    }

    @Override
    public Boolean fristSee(Long userId){
        Map<String,Object> userNoCondition = new HashMap<>(16);
        Map<String,Object> userNo = userNoService.getUserNoByUserId(userId);
        String step = (String) userNo.get("step");
        if(!Step.STEP_REVIEW_YES.equals(step)){
            return false;
        }
        userNoCondition.put("id",userId);
        userNoCondition.put("isFirst","N");
        return userNoService.updateUserNo(userNoCondition);
    }

    @Override
    public Map<String,Object> getMerchantInfo(Long id){
        Map<String,Object> merchantInfo = merchantInfoService.queryMerchantInfoById(id);
        merchantInfo.put("licensePicture",StringUtils.splitStr((String) merchantInfo.get("licensePicture")));
        merchantInfo.put("logo",StringUtils.splitStr((String) merchantInfo.get("logo")));
        merchantInfo.put("applyTime",DateUtils.parseLongToString((Long) merchantInfo.get("applyTime"),"yyyy-MM-dd"));
        return merchantInfo;
    }


    @Override
    public Boolean applyMerchant(MerchantInfoVo merchantInfoVo){
        Map<String,Object> merchantInfo = JavaBeanAndMapUtils.javaBeanToMap(merchantInfoVo);
        merchantInfo.put("licensePicture",StringUtils.combinationString((String[]) merchantInfo.get("licensePicture")));
        merchantInfo.put("logo",StringUtils.combinationString((String[]) merchantInfo.get("logo")));
        return merchantInfoService.addMerchantInfo(merchantInfo);
    }

    @Override
    public String queryProtocolByCondition(String type){
        if(type == null || "".equals(type)){
            return null;
        }
        return (String) protocolService.queryProtocolByCondition(type).get("content");
    }

    @Override
    public String getMerchantCooperationProtocol(String type){
        return queryProtocolByCondition(type);
    }

    @Override
    public CompanyInfoPo getMyCompanyInfo(Long id,String model){
        if(id == null){
            if(model == null || "".equals(model)){
                return null;
            }
        }
        return myCompanyInfoService.getMyCompanyInfo(id,model);
    }

    @Override
    public CompanyInfoPo getMerchantCooperationDescription(){
        return getMyCompanyInfo(null,"cooperationDescription");
    }

}
